package com.powerunion.datacollection.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.powerunion.datacollection.common.Constants;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.dao.ProcessInstanceDao;
import com.powerunion.datacollection.model.ActivityInstance;
import com.powerunion.datacollection.model.ItemInfo;
import com.powerunion.datacollection.model.jsonObject.JSONDataBean;
import com.powerunion.util.log.LogHelper;

public class ProcessInstanceManager {
	private static Logger logger = LogHelper.getInstance().getLogger(
			ProcessInstanceManager.class);
	private ProcessInstanceDao dao = null;

	public ProcessInstanceManager() {
		super();
		this.dao = (ProcessInstanceDao) DaoFactory.getInstance().getDao(
				ProcessInstanceDao.class);
	}

	/**
	 * 保存流程实例信息
	 * @param saveActivityInfoStr 环节信息
	 * @param currentUser 创建者
	 * @param saveItemInfoStr 流程实例信息
	 * @param procId 流程实例的原型流程定义信息Id
	 * @return
	 * @throws Exception
	 */
	public String save(String saveActivityInfoStr, String currentUser,String saveItemInfoStr,String procId,String exactlyTime) 
			throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(saveActivityInfoStr)) {
			String[] args = { "saveActivityInfoStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveActivityInfoStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		// 参数为空
		if (StringUtils.isEmpty(currentUser)) {
			String[] args = { "currentUser" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter currentUser NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		//参数为空
		if (StringUtils.isEmpty(saveItemInfoStr)) {
			String[] args = { "saveItemInfoStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveItemInfoStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		ItemManager im = new ItemManager();
	    String itemId = im.save(saveItemInfoStr, currentUser, Constants.PROINSTANCE);
	   
		Map activityInfoMap = new HashMap();
		activityInfoMap.put("data", ActivityInstance.class);
		JSONDataBean am = (JSONDataBean)JSONObject.toBean(JSONObject.fromObject(saveActivityInfoStr), JSONDataBean.class,activityInfoMap);
		List list = am.getData();
		boolean flag = false;
		if(!list.isEmpty()){
			ActivityInstance activityInfo = (ActivityInstance)list.get(0);
			flag = this.dao.save(activityInfo, itemId,procId,exactlyTime);
		}
		
		if(flag){
			return itemId;
		}else{
			return "";
		}
	}
	
	public String save(String saveActivityInfoStr, String currentUser,ItemInfo itemInfo,String procId,String exactlyTime)  throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(saveActivityInfoStr)) {
			String[] args = { "saveActivityInfoStr" };
			// 打印错误日志 
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveActivityInfoStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		// 参数为空
		if (StringUtils.isEmpty(currentUser)) {
			String[] args = { "currentUser" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter currentUser NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		//参数为空
		if (itemInfo == null) {
			String[] args = { "itemInfo" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter itemInfo NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		// 参数为空
		if (StringUtils.isEmpty(exactlyTime)) {
			String[] args = { "exactlyTime" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter exactlyTime NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		ItemManager im = new ItemManager();
	    String itemId = im.save(itemInfo);
	   
		Map activityInfoMap = new HashMap();
		activityInfoMap.put("data", ActivityInstance.class);
		JSONDataBean am = (JSONDataBean)JSONObject.toBean(JSONObject.fromObject(saveActivityInfoStr), JSONDataBean.class,activityInfoMap);
		List list = am.getData();
		boolean flag = false;
		if(!list.isEmpty()){
			ActivityInstance activityInfo = (ActivityInstance)list.get(0);
			flag = this.dao.save(activityInfo, itemId,procId,exactlyTime);
		}
		
		if(flag){
			return itemId;
		}else{
			return "";
		}
	}
	
	public List getProcessInfo(String processId)throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(processId)) {
			String[] args = { "processId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter processId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.getProcessInfo(processId);
	}
	
	public boolean initSubmissionWorkItems(String processId,String formId) throws Exception{
		
		// 参数为空
		if (StringUtils.isEmpty(processId)) {
			String[] args = { "processId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter processId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		// 参数为空
		if (StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.initSubmissionWorkItems(processId, formId);
	}

	/**
	 * 根据流程id复制该流程信息产生一条流程实例信息
	 * @param procId
	 * @param currentUser
	 * @return
	 * @throws Exception
	 */
	public String copyProcInfo(String procId,String currentUser,String exactlyTime) throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(procId)) {
			String[] args = { "procId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter procId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		String procInstanceId = "";
		ItemManager itm = new ItemManager();
		//查找流程定义信息
		ItemInfo it = itm.findByKey(procId);
		it.setId(null);
		//保存流程实例的定义信息，并记录返回的id
		it.setCrtDate(new Date());
		it.setModiDate(new Date());
		it.setItemType(Constants.PROINSTANCE);
		it.setCrtUser(currentUser);
		procInstanceId = itm.save(it);
		//得到流程定义的环节信息
		ActivityManager pim = new ActivityManager();
		List activityList = pim.findAllActivityByProcId(procId);
		boolean flag = this.dao.saveProcessInfo(activityList, procId, procInstanceId,currentUser,exactlyTime);
		
		if(flag){
			return procInstanceId;
		}else{
			return "";
		}
	}
	
	public String getProcDefineId(String itemId) throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(itemId)) {
			String[] args = { "itemId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter itemId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getProcDefineId(itemId);
	}
	
	public boolean delete(String itemId) throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(itemId)) {
			String[] args = { "itemId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter itemId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.delete(itemId);
	}
	
	public String validateActivityInfo(String procId) throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(procId)) {
			String[] args = { "procId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter procId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.validateActivityInfo(procId);
	}
	
	public List getProcessSurveillanceInfo(String processId) throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(processId)) {
			String[] args = { "processId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter processId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getProcessSurveillanceInfo(processId);
	}
	
	public List getTaskDetails(String processId) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(processId)) {
			String[] args = { "processId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter processId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getTaskDetails(processId);
	}
	
	
}
